package com.example.qxfw.ybzz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.qxfw.common.entity.zy.TrafficFcst;
import com.example.qxfw.ybzz.mapper.TrafficFcstMapper;
import com.example.qxfw.ybzz.service.TrafficFcstService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class TrafficFcstServiceImpl implements TrafficFcstService {

    @Autowired
    private TrafficFcstMapper trafficFcstMapper;

    @Override
    public List<TrafficFcst> listByParam(String dateChar, String timeChar, Integer nTimes1, Integer nTimes2, String stationId) {
        List<TrafficFcst> trafficFcsts = trafficFcstMapper.selectList(Wrappers.<TrafficFcst>query()
                .select("stationid", "stationname", "ntimes", "Longitude", "Latitude", "rain", "Weather1", "CAST(cast(WindS AS VARCHAR) as numeric(18,1)) as WindS", "CAST(cast(WindD AS VARCHAR) as numeric(18,1)) as WindD", "CAST(cast(Humid AS VARCHAR) as numeric(18,1)) as Humid,maxtemp,mintemp,cloud,cloudlow")
                .lambda()
                .eq(TrafficFcst::getDatechar, Timestamp.valueOf(dateChar))
                .eq(TrafficFcst::getTimechar, timeChar)
                .and(w -> w.eq(TrafficFcst::getNtimes, nTimes1)
                        .or()
                        .eq(TrafficFcst::getNtimes, nTimes2))
                .eq(TrafficFcst::getStationid, stationId)
                .eq(TrafficFcst::getForecasttype, 12)
                .orderByAsc(TrafficFcst::getStationid, TrafficFcst::getNtimes));
        List<TrafficFcst> list = trafficFcsts.stream().map(
                i -> {
                    TrafficFcst bo = new TrafficFcst();
                    BeanUtils.copyProperties(i, bo);
                    String weather1 = bo.getWeather1().replaceAll(" ", "");
                    bo.setWeather1(weather1);
                    return bo;
                }
        ).collect(Collectors.toList());
        return list;
    }

    @Override
    public Integer delete(String forecastTime, String strTimeChar, String stationId) {
        LambdaQueryWrapper<TrafficFcst> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TrafficFcst::getDatechar, forecastTime)
                .eq(TrafficFcst::getTimechar, strTimeChar)
                .eq(TrafficFcst::getStationid, stationId);
        int rows = trafficFcstMapper.delete(queryWrapper);
        return rows;
    }

    @Override
    public Integer save(TrafficFcst trafficFcst) {
        int rows = trafficFcstMapper.insert(trafficFcst);
        return rows;
    }
}
